package com.chang.demo;

import com.chang.demo.controller.TestController;
import com.chang.demo.utils.ControllerParser;
import com.chang.demo.utils.FileUtil;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import springfox.documentation.spring.web.plugins.Docket;

import javax.annotation.Resource;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;
import java.net.URL;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.function.Consumer;

@SpringBootApplication
public class DemoApplication {

	public static final ControllerParser controllerParserService = new ControllerParser();

	public static void main(String[] args) throws ClassNotFoundException {

		String path = Paths.get("main/java").toAbsolutePath().toString();


		SpringApplication.run(DemoApplication.class, args);
	}

	public static void test2 () throws ClassNotFoundException {
		Class<?> aClass = Class.forName("com.chang.demo.User");
		Field[] declaredFields = aClass.getDeclaredFields();
		for (Field field : declaredFields) {
			System.out.println(field.getName());
		}
	}

	public static void test1 () {
		Method[] methods = TestController.class.getDeclaredMethods();
		for (Method method : methods) {
			String name = method.getName();
			ApiOperation apiOperation = method.getAnnotation(ApiOperation.class);
			RequestMapping requestMapping = method.getAnnotation(RequestMapping.class);
			String[] path = requestMapping.path();
			RequestMethod[] requestMethods = requestMapping.method();

			String value = "";
			String[] tags = {""};

			if (apiOperation != null) {
				value = apiOperation.value();
				tags = apiOperation.tags();
			}
			Class<?> returnType = method.getReturnType();

			for (TypeVariable<Method> typeParameter : method.getTypeParameters()) {
				String parameterName = typeParameter.getName();
				String typeName = typeParameter.getTypeName();
				RequestBody requestBody = typeParameter.getAnnotation(RequestBody.class);
				System.out.println(String.format("方法%s: 参数:%s 参数类型: %s", name, parameterName, typeName));
				if (requestBody != null) {
					System.out.println(String.format("请求体类型: %s", requestBody.getClass().getName()));
				}
			}

			System.out.println(String.format("解析%s.%s, 请求方式: %s, 路径: %s, 说明: %s, 备注: %s", TestController.class.getName(), name, requestMethods[0].toString(), path[0], value, tags[0]));
			System.out.println("返回类型: " + returnType.getName());
		}
	}

}
